var flag = true;
var monstorArr = ["fire.png","trash.png","monster.png", "zombie.png", "ghost.png", "ghost1.png", "monster1.png"];
//定时器定义
var moveTimer;
var moveMonsterTimer;
var checkGameOverTimer;
var score = 0;
$(document).ready(function(){
	initMonsterOnePos();
	initMonsterTwoPos();
	$("#restart").click(function () {
		startGame();
	})
});
//设置定时器
$(document).keyup(function(e) {
	var keyCode = e.keyCode;
	//	  alert(keyCode);
	switch(keyCode) {
		case 32:
			if(flag){
				flag = false;
				moveAnimation();
				moveTimer = setInterval(moveAnimation, 3500);
				monsterMoveAnimation();
				moveMonsterTimer = setInterval(monsterMoveAnimation, 5000);
				checkGameOverTimer = setInterval(checkGameOver, 1);
				changeAlertMessage();
			}else{
				jump();
			}
			break;
		case 38:
			jump();
			break;
		default:
			break;
	}
});

//皮卡丘跳
function jump() {
	var jumpMusic = document.getElementById("jumpMusic");
	jumpMusic.play();
	$("#npc").animate({
		"top": "30px"
	}, 300, function() {
		$("#npc").animate({
			"top": "30px"
		},60, function () {
			$("#npc").animate({
				"top": "100px"
			}, 300);
		});
	});
}
//草地移动
function moveAnimation() {
	var groundDiv = $(".grassDivClass");
	$(groundDiv).animate({
		"left": "-" + $(groundDiv).width()/2 + "px"
	}, 3500,"linear" , function() {
		groundDiv.css("left","0px");
	});
}
//初始化怪物位置
function initMonsterOnePos(){
	var firstPos = parseInt(Math.random()*300+450);
	var firstMonster = parseInt(Math.random()*monstorArr.length);
	//第一只怪物
	$("#roadblock1").css("margin-left",firstPos+"px");
	$("#roadblock1 img").attr("src","img/"+monstorArr[firstMonster]);
}
function initMonsterTwoPos() {
	var secondPos = parseInt(Math.random()*300+450);
	var secondMonster = parseInt(Math.random()*monstorArr.length);
	//第二只怪物
	$("#roadblock2").css("margin-left",secondPos+"px");
	$("#roadblock2 img").attr("img","img/"+monstorArr[secondMonster]);
}
// 怪物移动
function monsterMoveAnimation() {
	var roadBlock = $("#roadblock");

	$(roadBlock).animate({
		"left":"-" + $(roadBlock).width()/2 + "px"
	},2500,"linear", function () {
		$(roadBlock).animate({
			"left":"-" + $(roadBlock).width()-100 + "px"
		},2500,"linear", function () {
			initMonsterOnePos();
			initMonsterTwoPos();
			$(roadBlock).css("left","0px");
		});
	});
}
//检查是否结束游戏
function checkGameOver() {
	score+=0.1;
	var roadBlock = $("#roadblock");
	var block1 = $("#roadblock1");
	var block2 = $("#roadblock2");
	//两个怪物距离npc的距离
	var firstBL = parseFloat($(roadBlock).css("left"))+parseFloat($(block1).css("margin-left"));
	var secondBL = parseFloat($(roadBlock).css("left"))+parseFloat($(block1).css("margin-left"))+parseFloat($(block2).css("margin-left"))+35;

	//npc的位置
	var npcW = parseFloat($("#npc").css("width"));
	var npcH = parseFloat($("#npc").css("height"));
	var npcL = parseFloat($("#npc").css("left"));
	var npcT = parseFloat($("#npc").css("top"));

//	判断
	if (firstBL <= npcL+npcW-30  && firstBL>=npcL){
		if(npcT>70){
		//	失败
			gameOver();
		}
	}
	if (secondBL <= npcL+npcW-30  && secondBL>=npcL){
		if(npcT>70){
			//	失败
			gameOver();
		}
	}

	$("#scoreSpan").html(parseInt(score));
}
//游戏结束
function gameOver() {
	var gameOverMusic = document.getElementById("gameOverMusic");
	gameOverMusic.play();
	//暂停所有操作
	clearInterval(moveTimer);
	$(".grassDivClass").stop();
	clearInterval(moveMonsterTimer);
	$("#roadblock").stop();
	clearInterval(checkGameOverTimer);
//	弹出游戏结束框
// 	setTimeout(startGame, 1000);
	$("#restart").toggle();
}
function changeAlertMessage() {
	if(!flag) {
		$("#p2").text("点击空格或方向上键跳跃")
	}else{
		$("#p2").text("请点击空格开始游戏");
	}
}
//重新开始
function startGame() {
	score = 0;
	$("#scoreSpan").text(score);
	$("#roadblock").css("left","0px");
	$(".grassDivClass").css("left","0px");
	initMonsterOnePos();
	initMonsterTwoPos();
	flag = true;
	changeAlertMessage();
	$("#restart").toggle();
}